package org.example.backend.Module.School.School.Controller;

import org.example.backend.Base.Response.JsonResult;
import org.example.backend.Module.School.School.Pojo.Entity.School;
import org.example.backend.Module.School.School.Service.SchoolService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/v1/school/school")
public class SchoolController {

    @Resource
    private SchoolService schoolService;

    @GetMapping("list")
    public JsonResult list(@RequestParam(required = false) String name, @RequestParam(required = false) String id) {
        return JsonResult.ok(schoolService.list(name, id));
    }

    @PostMapping("add")
    public JsonResult add(@RequestBody @Validated(value = School.OnAdd.class) School school) {
        schoolService.addOrUpdate(school);

        return JsonResult.ok();
    }

    @PostMapping("update")
    public JsonResult update(@RequestBody @Validated(value = School.OnUpdate.class) School school) {
        schoolService.addOrUpdate(school);

        return JsonResult.ok();
    }

    @GetMapping("delete/{id}")
    public JsonResult delete(@PathVariable Long id) {
        schoolService.delete(id);

        return JsonResult.ok();
    }
}
